﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CloudShot.Properties;
using CloudShot.Utils;

namespace CloudShot.Forms
{
  sealed partial class AboutBox : Form
  {
    public AboutBox()
    {
      InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      Text = string.Format("{0} {1}", AssemblyProduct, AssemblyVersion);

#if DEBUG
      Text += " (DEBUG)";
#endif

      lblProductName.Text = AssemblyProduct;
      //lblCompanyName.Text = AssemblyCompany;
      //txtBoxDescription.Text = AssemblyDescription;
      Icon = Resources.applicationIcon;

      toolTip.SetToolTip(lnkMail, AppHelper.FeedbackEmail);
      toolTip.SetToolTip(lnkHelp, AppHelper.HelpUrl);
      toolTip.SetToolTip(lnkWebsite, AppHelper.ProjectWebsiteUrl);
      toolTip.SetToolTip(lnkError, string.Format(Resources.AboutBox_ErrorsLinkTooltip, AppHelper.ErrorFile));

      //Set developer links
      SetDeveloperLinks();
    }

    private void SetDeveloperLinks()
    {
      developersLink.Text = Resources.AboutBox_CreatedBy + @" ";
      List<KeyValuePair<string, Uri>> developers = AppHelper.Developers;

      for (int i = 0; i < developers.Count; i++)
      {
        KeyValuePair<string, Uri> developer = developers[i];
        bool isLast = (i == developers.Count - 1);
        string separator;

        if (isLast)
        {
          developersLink.Text = developersLink.Text.TrimEnd(',', ' ');
          developersLink.Text += " and ";
          separator = ".";
        }
        else
        {
          separator = ", ";
        }

        int start = developersLink.Text.Length;
        developersLink.Text += string.Format("{0}{1}", developer.Key, separator);
        developersLink.Links.Add(new LinkLabel.Link(start, developer.Key.Length, developer.Value));
      }
    }

    private void lnkError_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      string errorFile = AppHelper.ErrorFile;

      if (File.Exists(errorFile))
        AppHelper.OpenInViewer(errorFile);
      else
        MessageBox.Show(Resources.AboutBox_NoErrorsFoundMessageBox, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

      Close();
    }

    private void lnkMail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      if (string.IsNullOrEmpty(AppHelper.FeedbackEmail))
        return;

      string mailto = string.Format("mailto://{0}?subject=Feedback for {1} {2}", AppHelper.FeedbackEmail, Application.ProductName, Application.ProductVersion);
      AppHelper.OpenInViewer(mailto);
      Close();
    }

    private void lnkHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      AppHelper.OpenInViewer(AppHelper.HelpUrl);
      Close();
    }

    private void lnkWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      AppHelper.OpenInViewer(AppHelper.ProjectWebsiteUrl);
      Close();
    }

    private void OnDevelopersLinkClicked(object sender, LinkLabelLinkClickedEventArgs args)
    {
      var link = args.Link.LinkData as Uri;

      if (link != null)
        AppHelper.OpenInViewer(link.AbsoluteUri);
    }

    #region Assembly Attribute Accessors

    public string AssemblyTitle
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
        if (attributes.Length > 0)
        {
          var titleAttribute = (AssemblyTitleAttribute)attributes[0];
          if (titleAttribute.Title != "")
          {
            return titleAttribute.Title;
          }
        }
        return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
      }
    }

    public string AssemblyVersion
    {
      get { return Application.ProductVersion; }
    }

    public string AssemblyDescription
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
        if (attributes.Length == 0)
        {
          return "";
        }
        return ((AssemblyDescriptionAttribute)attributes[0]).Description;
      }
    }

    public string AssemblyProduct
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
        if (attributes.Length == 0)
        {
          return "";
        }
        return ((AssemblyProductAttribute)attributes[0]).Product;
      }
    }

    public string AssemblyCopyright
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
        if (attributes.Length == 0)
        {
          return "";
        }
        return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
      }
    }

    public string AssemblyCompany
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
        if (attributes.Length == 0)
        {
          return "";
        }
        return ((AssemblyCompanyAttribute)attributes[0]).Company;
      }
    }

    #endregion
  }
}